
import {StaticGroup} from "../model/StaticGroup";
import AreaJson from '../json/AreaJson.json'

export class ModelService {

    public getData(host, options) {
        StaticGroup.isMock=true;
        if (options.dataViews.length == 0) {
            StaticGroup.bindPointData = StaticGroup.defData.map(x=>{
                x.show=true;
                return x;
            })
            this.areaInfoLabels();
            return;
        }
        const array = options.dataViews[0].plain.data;
        const numeric = options.dataViews[0].plain.profile.numeric.values[0].display;
        const category = options.dataViews[0].plain.profile.category.values[0].display;

        const start_lng = options.dataViews[0].plain.profile.start_lng.values.length > 0 ? options.dataViews[0].plain.profile.start_lng.values[0].display : "";
        const start_lat = options.dataViews[0].plain.profile.start_lat.values.length > 0 ? options.dataViews[0].plain.profile.start_lat.values[0].display : "";

        StaticGroup.format= "General";
        StaticGroup.displayUnit = options.dataViews[0].plain.profile.numeric.options.valueDisplayUnit;
        //组合数据点
        const pointData = [];
        array.forEach(x => {
            const {unit ,num}=this.formatNumber(x[numeric]);
            const area = AreaJson.find(xr => (xr.name.indexOf(x[category]) ==0 && x[category].length >= 2));
            const name=area?area.name: x[category];
            const adcode=area?area.adcode: x[category];
            if (start_lng != "") {
                pointData.push({
                    adcode:adcode,
                    show:this.showAreaOutData(area),
                    name: name,
                    center: [x[start_lng], x[start_lat]],
                    centroid: [x[start_lng], x[start_lat]],
                    value: num,
                    number:num,
                    unit:unit[0]
                });
            } else {
                if (area) {
                    pointData.push({
                        adcode:adcode,
                        show:this.showAreaOutData(area),
                        name: x[category],
                        center: area[0].center,
                        centroid: area[0].centroid,
                        value: num,
                        number:num,
                        unit:unit
                    });
                }
            }
        });


        if (array.length > 0) {
            const items = array.reduce((result: any, item: any, i: number) => {
                const selectionId = StaticGroup.host.selectionService.createSelectionId();
                const _irs = options.dataViews[0].plain.profile.category.values;
                selectionId.withDimension(options.dataViews[0].plain.profile.category.values[0], item);
                result.push({
                    name: item[_irs[0].display],
                    selectionId,
                });
                return result;
            }, []);
            StaticGroup.items = items;
        }
        if (pointData.length != 0) {
            StaticGroup.bindPointData = pointData;
            StaticGroup.isMock=false;
        }

        this.areaInfoLabels();
    }

    /**
     * 是否显示区域外的数据
     * @param area
     */
    public showAreaOutData(area){
        if (area.length>0){
            if (area[0].acroutes.indexOf(StaticGroup.adcode) >-1 || area[0].adcode==StaticGroup.adcode){
                return true;
            }
            return false;
        }
        return true;
    }
    /**
     * label之外的标签
     */
    public areaInfoLabels(){
        StaticGroup.otherAreaInfo=AreaJson.filter(xr => xr.parent==StaticGroup.adcode).map(x=>{
            return {
                adcode:x.adcode,
                name:x.name,
                center:x.centroid
            };
        });
    }


    /**
     * 数据格式化
     * @param number
     */
    public formatData = (number) => {
        const formatService = StaticGroup.host.formatService;
        let realDisplayUnit = StaticGroup.displayUnit == null ? "auto" : StaticGroup.displayUnit;
        if (formatService.isAutoDisplayUnit(realDisplayUnit)) {
            realDisplayUnit = formatService.getAutoDisplayUnit([number]);
        }
        return formatService.format(StaticGroup.format, number, realDisplayUnit);
    }

    /**
     * 将格式化后的字符串分开
     * @param number
     */
    public formatNumber(number){
        const fomat=this.formatData(number);
        const unit=this.extractChineseCharacters(fomat);
        const num=fomat.replace(unit,'');
        return { unit ,num };

    }
    /**
     * 获取字符串中的单位
     * @param inputString
     */
    public extractChineseCharacters(inputString) {
        // 使用正则表达式匹配中文字符
        const chak = inputString.match(/[\u4e00-\u9fa5]+/g);
        return chak?chak[0]:"";
    }

}